package org.geotools.tutorial.quickstart.UI;

import org.geotools.tutorial.quickstart.Utilities.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @DesktopJavaDocable disable
 */
public class DialogGeopackageLoader extends JDialog {

	private JLabel labelDatabase;
	private JTextField textFieldDatabase;

	private JButton buttonCancel;
	private JButton buttonOK;
	protected transient DialogResult dialogResult = DialogResult.NO;

	public Map getParam() {
		Map params = new HashMap();
		params.put("dbtype", "geopkg");
		params.put("database", this.textFieldDatabase.getText());//需要连接的数据库
		return params;
	}

	public DialogResult getDialogResult() {
		return this.dialogResult;
	}

	public DialogGeopackageLoader(JFrame owner) {
		super(owner, true);
		initComponent();
		initLayout();
	}

	public DialogResult showDialog() {
		try {
			setSize(350, 270);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.getDialogResult();
	}

	private void initComponent() {

		labelDatabase = new JLabel("Database:");
		textFieldDatabase = new JTextField();
		buttonOK = new JButton("Open");
		buttonCancel = new JButton("Cancel");
		textFieldDatabase.setText("G:\\数据\\Data\\china\\china.gpkg");
		setTitle("Open GeopackageDataStore");

		buttonOK.addActionListener((e) -> {
			this.dialogResult = DialogResult.OK;
			dispose();
		});
		buttonCancel.addActionListener((e) -> {
			this.dialogResult = DialogResult.CANCEL;
			dispose();
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		JPanel panelButton = new JPanel();
		panelButton.setLayout(new GridBagLayout());
		panel.add(this.labelDatabase, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldDatabase, new GridBagConstraintsHelper(1, 0, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 0).setWeight(1, 0));
		panelButton.add(this.buttonOK, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.NONE).setInsets(0, 15, 10, 10).setWeight(1, 1));
		panelButton.add(this.buttonCancel, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.NONE).setInsets(0, 0, 10, 10).setWeight(0, 1));
		this.add(panel, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0).setInsets(5, 10, 5, 0));
		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
		this.add(panelButton, new GridBagConstraintsHelper(0, 2, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0));
	}

	public enum DialogResult {
		OK,
		CANCEL,
		NO,
	}


}
